<template>
  <el-dialog
    v-model="dialogVisible"
    title="删除原因"
    width="450px"
    @close="closeDialog"
  >
    <div>确定删除报废原因{{ state.form.reasonName }}吗？</div>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="deleteBtn">删除</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { deleteRepairReasonApi } from '@/api/equip/equip_api'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const dialogVisible = ref(false)
  const state = reactive({
    form: {},
  })

  const openDialog = async (rowData) => {
    state.form = rowData
    dialogVisible.value = true
  }

  const deleteBtn = async () => {
    const load = $baseLoading()
    try {
      let params = {
        pkRepairReason: state.form.pkRepairReason,
      }
      const { code, msg } = await deleteRepairReasonApi(params)
      if (code === 2000) {
        $baseMessage(msg, 'success')
        dialogVisible.value = false
        _emit('success-callback')
      } else {
        $baseMessage(msg, 'error')
      }
    } finally {
      load.close()
    }
  }

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
